package cn.ffcs.uec.outStaff.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffContractEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffContract;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

/**
 * @Title:外包人员合同
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年9月19日
 */
@Controller
@Scope("prototype")
public class UecOutStaffContractEditComposer extends BasePortletComposer {

    private static final long serialVersionUID = -6096343177536382090L;

    private UecOutStaffContractEditBean bean = new UecOutStaffContractEditBean();

    private String opType;

    @Resource
    private IdentityCardConfigManager identityCardConfigManager;

    private UecOutStaffContract uecOutStaffContract = new UecOutStaffContract();

    private UecOutStaffInfo uecOutStaffInfo = new UecOutStaffInfo();

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        Components.wireVariables(comp, bean);
    }

    /**
     * window初始化.
     * 
     * @throws Exception
     *             异常
     */
    public void onCreate$uecOutStaffContractEditWin() throws Exception {
        bindCombobox();
        opType = (String) arg.get("opType");
        uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
        if (opType.equals("add")) {
            // 将实际终止日期隐藏
            bean.getActualEndDateLabel().setVisible(false);
            bean.getActualEndDate().setVisible(false);
            bean.getUecOutStaffContractEditWin().setTitle("外包人员合同新增");
        } else if (opType.equals("edit")) {
            uecOutStaffContract = (UecOutStaffContract) arg.get("uecOutStaffContract");
            bean.getUecOutStaffContractEditWin().setTitle("外包人员合同修改");
            strToDate();
        } else if (opType.equals("view")) {
            bean.getUecOutStaffContractEditWin().setTitle("外包人员合同查看");
            strToDate();
            PubUtil.disableComponent(bean.getUecOutStaffContractEditWin());
        }
    }

    /**
     * 保存.
     * 
     * @throws Exception
     */
    public void onOk() throws Exception {
        String msg = checkUacStaffContractData();
        if (msg != null) {
            Messagebox.show(msg);
            return;
        }
        if (opType.equals("add")) {
            // 新增时需要将合同中最新的一条数据的合同实际终止日期置为新数据的合同的开始时间
            uecOutStaffContract = UecOutStaffContract.newInstance();
            PubUtil.fillPoFromBean(bean, uecOutStaffContract);
            uecOutStaffContract.setCtBeginDate(DateUtil.dateToStr(this.bean.getCtBeginDate().getValue()));
            uecOutStaffContract.setCtEndDate(DateUtil.dateToStr(this.bean.getCtEndDate().getValue()));
//            uecOutStaffContract.setCtCode(bean.getCtCode().getValue());
//            uecOutStaffContract.setTrialPeriod(bean.getTrialPeriod().getValue());
//            uecOutStaffContract.setCtMajor(bean.getCtMajor().getValue());
//            uecOutStaffContract.setActualEndDate(null);
            uecOutStaffContract.setDr(1L);
            uecOutStaffContract.setNewMark("Y");
            // 试用期限
            if (!StrUtil.isNullOrEmpty(uecOutStaffInfo)) {
                uecOutStaffContract.setOutStaffId(uecOutStaffInfo.getOutStaffId());
                uecOutStaffContract.setPkPsnMsg(uecOutStaffInfo.getPkPsnMsg());
            }
            uecOutStaffContract.addOnly();
            Events.postEvent("onOK", this.self, uecOutStaffContract);
            onCancel();
            Messagebox.show("新增成功！");
        } else if (opType.equals("edit")) {
            PubUtil.fillPoFromBean(bean, uecOutStaffContract);
            uecOutStaffContract.setCtBeginDate(DateUtil.dateToStr(this.bean.getCtBeginDate().getValue()));
            uecOutStaffContract.setCtEndDate(DateUtil.dateToStr(this.bean.getCtEndDate().getValue()));
            uecOutStaffContract.setActualEndDate(DateUtil.dateToStr(this.bean.getActualEndDate().getValue()));
            //uecOutStaffContract.setDr(1L);
            if (!StrUtil.isNullOrEmpty(uecOutStaffInfo)) {
                uecOutStaffContract.setOutStaffId(uecOutStaffInfo.getOutStaffId());
            }
            uecOutStaffContract.updateOnly();
            Events.postEvent("onOK", this.self, uecOutStaffContract);
            onCancel();
            Messagebox.show("修改成功！");
        }
        onCancel();
    }

    /**
     * 取消.
     */
    public void onCancel() {
        bean.getUecOutStaffContractEditWin().onClose();
    }

    private String checkUacStaffContractData() {
        // BaseInfo Check
        /*
         * if (StrUtil.isNullOrEmpty(bean.getJobName().getValue())) { return
         * "请填写岗位名称"; } else if
         * (StrUtil.checkBlank(bean.getJobName().getValue())) { return
         * "岗位名称中有空格"; } else if
         * (StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) { return
         * "请填写在本单位从事外包开始日期"; }
         */
        return null;
    }

    /**
     * 字符串转date
     */
    private void strToDate() {
        if (arg.get("uecOutStaffContract") != null) {
            uecOutStaffContract = (UecOutStaffContract) arg.get("uecOutStaffContract");
            PubUtil.fillBeanFromPo(uecOutStaffContract, bean);
            if (!StrUtil.isEmpty(uecOutStaffContract.getCtBeginDate())) {
                bean.getCtBeginDate().setValue(DateUtil.patternStrToDate(uecOutStaffContract.getCtBeginDate()));
            }
            if (!StrUtil.isEmpty(uecOutStaffContract.getCtEndDate())) {
                bean.getCtEndDate().setValue(DateUtil.patternStrToDate(uecOutStaffContract.getCtEndDate()));
            }
            if (!StrUtil.isEmpty(uecOutStaffContract.getActualEndDate())) {
                bean.getActualEndDate().setValue(DateUtil.patternStrToDate(uecOutStaffContract.getActualEndDate()));
            }
        }
    }

    /**
     * 绑定下拉框 绑定combobox
     */
    private void bindCombobox() {
        // 合同期限类型
        ListboxUtils.rendererForEdit(bean.getPkTermtype(),
                NodeVoUtil.getDictionaries(UecConstants.CONTRACT_DURATION_TYPE));
        // 合同变动类型
        ListboxUtils.rendererForEdit(bean.getCtChangeType(),
                NodeVoUtil.getDictionaries(UecConstants.CONTRACT_CHANGE_TYPE));
        // 工时制
        ListboxUtils.rendererForEdit(bean.getWorkhourSystem(),
                NodeVoUtil.getDictionaries(UecConstants.CONTRACT_WORK_HOUR_SYSTEM));
        // 是否小CEO
        List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
        String[][] isSend = { { "Y", "是" }, { "N", "否" } };
        for (int i = 0; i < isSend.length; i++) {
            NodeVo nodeVo = new NodeVo();
            for (int j = 0; j < 1; j++) {
                nodeVo.setId(isSend[i][j]);
                nodeVo.setName(isSend[i][j + 1]);
            }
            nodeVoIsSendList.add(nodeVo);
        }
        ListboxUtils.rendererForEdit(bean.getNewMark(), nodeVoIsSendList);
    }
}
